package com.wex.cookbook.controller.adminController;

import com.wex.cookbook.config.base.Result;
import com.wex.cookbook.dto.to.userInfo.UserInfoDto;
import com.wex.cookbook.entity.Userinfo;
import com.wex.cookbook.service.UserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.List;

/**
 * @author : wex
 * @createTime : 2023/10/19 10:39
 */
@Api(tags = "管理员用户信息管理")
@RestController
@RequestMapping("/admin/userInfo")
public class AdminUserInfoController {

    @Autowired
    private UserinfoService userinfoService;

    @PreAuthorize(value = "hasRole('ADMIN')")
    @ApiOperation(value = "查看所用用户信息")
    @GetMapping("/getAllUserInfo")
    public Result<List<UserInfoDto>> getAllUserInfo() {
        List<Userinfo> list = userinfoService.list();
        List<UserInfoDto> userInfoDtoList = new ArrayList<>();
        for (Userinfo userinfo : list) {
            UserInfoDto userInfoDto = new UserInfoDto();
            BeanUtils.copyProperties(userinfo, userInfoDto);
            userInfoDtoList.add(userInfoDto);
        }
        return Result.success(userInfoDtoList);
    }

    @PreAuthorize(value = "hasRole('ADMIN')")
    @ApiOperation(value = "删除用户信息")
    @GetMapping("/deleteUserInfo/{username}")
    public Result<String> deleteUserInfo(@PathVariable String username) {
        return userinfoService.deleteUserInfo(username);
    }

}
